<?php
/**
 *实名认证
 * @author yanghj 2014.5.22
 */
class CertificationApp extends MemberbaseApp
{
    var $_user_id;
    var $_certification_mod;
    var $_user_mod;
    var $_member_from;

    function __construct()
    {
        $this->Account_security();
    }
    function Account_security()
    {
        parent::__construct();
        $this->_user_id = intval($this->visitor->get('user_id'));
        $this->_certification_mod = &m('certification');
        $this->_user_mod = &m('member');
        $this->_member_from = isset($_GET['from']) ? trim($_GET['from']) : 'buyer';
        $this->assign('member_from', $this->_member_from);

    }

    function index()
    {
        $accout_data = $this->_certification_mod->get(array('fields' => 'certification_type,status', 'conditions'    => 'user_id = ' . $this->_user_id, 'order' => 'certification_id desc'));
        if($accout_data && $accout_data['status'] != -1)
        {
            if($accout_data['certification_type'] == 1 && $accout_data['status'] == 0)
            {
                header("Location:index.php?app=certification&act=profile&step=3&from=" . $this->_member_from);
                return;               
            }
            if($accout_data['certification_type'] == 2 && $accout_data['status'] == 0)
            {
                header("Location:index.php?app=certification&act=company&step=3&from=" . $this->_member_from);  
                return;             
            }
            if($accout_data['status'] == 1)
            {
                $this->show_warning('您已经申请过了实名认证,不能重复申请!','返回安全设置','index.php?app=account_security&from=' . $this->_member_from);
                return;
                
            }
            
        }
        if ($this->_member_from == 'seller') {
            $this->_curlocal(LANG::get('seller_center'), 'index.php?app=seller', 
                             LANG::get('my_account'),'index.php?app=account_security&from=seller',
                             '实名认证');
            $this->_config_seo('title', Lang::get('seller_center') . ' - 实名认证');
        } else {
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', 
                             LANG::get('my_account'),'index.php?app=account_security&from=buyer', 
                             '实名认证');
            $this->_config_seo('title', Lang::get('buyer_center') . ' - 实名认证');
        }
        $article_mod =& m('article');
        $artile_list = $article_mod->find(
            array(
    		'conditions'    => "cate_id=27 AND if_show =1",
    		'fields'        => 'title, cate_id, link',
    		'limit'         => 4,
    		'order'         => 'sort_order ASC, add_time DESC',      
            ));
        
        /* 当前用户中心菜单 */
        $this->_curitem('my_safety');

        /* 当前所处子菜单 */
        $this->_curmenu('certification');
        $this->assign('artile_list', $artile_list);      
        $this->display('certification.index.html');
        

    }
    
    /**
     *    个人认证
     *
     */
    function profile(){
        $step = isset($_GET['step']) ? intval($_GET['step']) : 1;
        $accout_data = $this->_certification_mod->get(array('fields' => 'certification_type,status', 'conditions'    => 'user_id = ' . $this->_user_id, 'order' => 'certification_id desc'));
        if($accout_data && $accout_data['status'] != -1)
        {
            if($step != 3)
            {
                $this->show_warning('您已经申请过了实名认证,不能重复申请!','返回安全设置','index.php?app=account_security&from=' . $this->_member_from);
                return;               
            }           
            
        }
        /* 当前位置 */           
        if ($this->_member_from == 'seller') {
        $this->_curlocal(LANG::get('seller_center'), 'index.php?app=seller', 
                         LANG::get('my_account'),'index.php?app=account_security&from=seller',
                         '实名认证');
        $this->_config_seo('title', Lang::get('seller_center') . ' - 实名认证');
        } else {
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', 
                             LANG::get('my_account'),'index.php?app=account_security&from=buyer', 
                             '实名认证');
            $this->_config_seo('title', Lang::get('buyer_center') . ' - 实名认证');
        }
       
        /* 当前用户中心菜单 */
        $this->_curitem('my_safety');

        /* 当前所处子菜单 */
        $this->_curmenu('certification');
        
        if ($step ==1)
        {
            include(ROOT_PATH . '/includes/cfg_status.php');
            if (!IS_POST)
            {
                $data = array();
                if($_SESSION['certification_profile'])
                {
                    $data = ecm_json_decode($_SESSION['certification_profile'],1);
                }                
                $this->import_resource(array(
                    'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,mlselection.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,ajaxupload.js',
                    'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css',
                ));
                $this->_get_regions();  
                $this->assign('cfg_bank_name', $cfg_bank_name);
                $this->assign('form_data', $data);
                $this->assign('user_name', $this->visitor->get('user_name'));                     
                $this->display('certification.profile1.html');
                
            }
            else
            {                
                $data = array(
                'user_id' => $this->_user_id,
                'user_name' => $this->visitor->get('user_name'),
                'real_name' => html_script(trim($_POST['real_name'])),
                'identity_card' => html_script(trim($_POST['identity_card'])),
                'identity_end_data' => trim($_POST['identity_end_data']),
                'bank_name' => html_script(trim($_POST['bank_name'])),
                'region_id' => (int)$_POST['region_id'],
                'region_name' =>trim($_POST['region_name']),
                'bank_address' => html_script(trim($_POST['bank_address'])),
                'bank_card' => html_script(trim($_POST['bank_card'])),
                'add_time' => gmtime(),
                'certification_type' => 1,
                'status' => 0,
              );
              if(!$data['real_name'] || !$data['identity_card'] || !$data['identity_end_data'] || !$data['bank_name'] || !$data['region_name'] || !$data['bank_card'] || !$_POST['identity_img1'] || !$_POST['identity_img2'])
              {
                $this->show_warning('数据不完整,请重新填写!');   
                 return;           
              }
              if(!is_idcard($data['identity_card']))
              {
                $this->show_warning('身份证号码不正确!');
                 return;
              }
             if (!empty($_POST['identity_img1']))
             {
            	$data['identity_img1'] = $this->upload_copy_img($_POST['identity_img1'],'identity_img1');
            	if ($data['identity_img1'] === false)
            	{
            	   $this->show_warning('上传图片失败!');
            		return;
            	}
             }
              if (!empty($_POST['identity_img2']))
             {
            	$data['identity_img2'] = $this->upload_copy_img($_POST['identity_img2'],'identity_img2');
            	if ($data['identity_img2'] === false)
            	{
            	   $this->show_warning('上传图片失败!');
            		return;
            	}
             }
            
              $_SESSION['certification_profile'] = ecm_json_encode($data);
              $this->assign('info', $data);
              $this->assign('cfg_bank_name', $cfg_bank_name); 
              $this->display('certification.profile2.html');
                
            }
            
        }
        if($step == 3)
        {
            if($_SESSION['certification_profile'])
            {
               $data = ecm_json_decode($_SESSION['certification_profile'],1);
               $this->_certification_mod->add($data);
                if ($this->_certification_mod->has_error())
                {
                    $this->show_warning($this->_certification_mod->get_error());    
                    return;
                }
                unset($_SESSION['certification_profile']);               
            }
            $this->display('certification.profile3.html');
            
        }
        
    }
    
    /**
     *    企业认证
     *
     */
    function company(){
        $step = isset($_GET['step']) ? intval($_GET['step']) : 1;
        $accout_data = $this->_certification_mod->get(array('fields' => 'certification_type,status', 'conditions'    => 'user_id = ' . $this->_user_id, 'order' => 'certification_id desc'));
        if($accout_data)
        {
            if($step != 3 && $accout_data['status'] != -1)
            {
                $this->show_warning('您已经申请过了实名认证,不能重复申请!','返回安全设置','index.php?app=account_security&from=' . $this->_member_from);
                return;               
            }           
            
        }
        /* 当前位置 */           
        if ($this->_member_from == 'seller') {
        $this->_curlocal(LANG::get('seller_center'), 'index.php?app=seller', 
                         LANG::get('my_account'),'index.php?app=account_security&from=seller',
                         '实名认证');
        $this->_config_seo('title', Lang::get('seller_center') . ' - 实名认证');
        } else {
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', 
                             LANG::get('my_account'),'index.php?app=account_security&from=buyer', 
                             '实名认证');
            $this->_config_seo('title', Lang::get('buyer_center') . ' - 实名认证');
        }
       
        /* 当前用户中心菜单 */
        $this->_curitem('my_safety');

        /* 当前所处子菜单 */
        $this->_curmenu('certification');
        
        if ($step ==1)
        {
            include(ROOT_PATH . '/includes/cfg_status.php');
            if (!IS_POST)
            {
                $data = array();
                if($_SESSION['certification_company'])
                {
                    $data = ecm_json_decode($_SESSION['certification_company'],1);
                }
                $this->import_resource(array(
                    'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,mlselection.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,ajaxupload.js',
                    'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css',
                ));
                $this->_get_regions();  
                $this->assign('cfg_bank_name', $cfg_bank_name);
                $this->assign('form_data', $data);
                $this->assign('user_name', $this->visitor->get('user_name'));     
                $this->display('certification.company1.html');
                
            }
            else
            {                
                $data = array(
                'user_id' => $this->_user_id,
                'user_name' => $this->visitor->get('user_name'),
                'company_name' => html_script(trim($_POST['company_name'])),
                'license_card' => html_script(trim($_POST['license_card'])),
                'license_end_data' => trim($_POST['license_end_data']),
                'bank_name' => html_script(trim($_POST['bank_name'])),
                'region_id' => (int)$_POST['region_id'],
                'region_name' =>trim($_POST['region_name']),
                'bank_address' => html_script(trim($_POST['bank_address'])),
                'bank_card' => html_script(trim($_POST['bank_card'])),
                'add_time' => gmtime(),
                'certification_type' => 2,
                'status' => 0,
              );
              if(!$data['company_name'] || !$data['license_card'] || !$data['license_end_data'] || !$data['bank_name'] || !$data['region_name'] || !$data['bank_address'] || !$data['bank_card'] || !$_POST['license_img'])
              {
                $this->show_warning('数据不完整,请重新填写!');   
                 return;           
              }
              if (!empty($_POST['license_img']))
              {
            	$data['license_img'] = $this->upload_copy_img($_POST['license_img'],'license_img');
            	if ($data['license_img'] === false)
            	{
            	   $this->show_warning('上传图片失败!');
            		return;
            	}
              }
              $_SESSION['certification_company'] = ecm_json_encode($data);
              $this->assign('info', $data);
              $this->assign('cfg_bank_name', $cfg_bank_name);               
              $this->display('certification.company2.html');
                
            }
            
        }
        if($step == 3)
        {
            if($_SESSION['certification_company'])
            {
               $data = ecm_json_decode($_SESSION['certification_company'],1);
               $this->_certification_mod->add($data);
                if ($this->_certification_mod->has_error())
                {
                    $this->show_warning($this->_certification_mod->get_error());    
                    return;
                }
                unset($_SESSION['certification_company']);               
            }
            $this->display('certification.company3.html');
            
        }
        
    }
    
    /**
     * 复制ajax上传的图片
     *
     * @param string $src
     * @return mix false表示复制失败,string表示复制文件地址
     */
	function upload_copy_img($src,$file_name)
	{
		if (!file_exists($src))
		{
			return false;
		}
        import('uploader.lib');
		$uploader = new Uploader();
        $arr = pathinfo($src);
        $extension = $arr['extension'];
        $target = 'data/files/mall/certification/' . ceil($this->_user_id / 500).$file_name.$this->_user_id.'.'.$extension;
        $file_url = $uploader->copy_uploaded_file($src, $target);
        unlink($src);
        return $file_url;

	}
    
    /**
     * 上传图片
     *
     * @param $_FILES $file  文件来源
     * @param int $user_id   用户ID 
     * @param string $name   文件名 
     * @return mix false表示上传失败,空串表示没有上传,string表示上传文件地址
     */
    function _upload_img($file,$user_id,$name)
    {
        if ($file['error'] != UPLOAD_ERR_OK)
        {
            return '';
        }
        import('uploader.lib');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE);
        $uploader->addFile($file);
        if ($uploader->file_info() === false)
        {
            $this->show_warning($uploader->get_error());
            return false;
        }
        $uploader->root_dir(ROOT_PATH);
        return $uploader->save('data/files/mall/certification/' . ceil($user_id / 500), $name.$user_id);
    }   
    
    /**
     *    获取区域信息
     *
     */
    function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(0);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }


}

?>